package cn.aiyuan;

import com.sun.istack.internal.NotNull;
import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author wangyuan
 * @date 2021/8/3
 */
@Data
public class DelayedUser implements Delayed {

    private String name;

    private long aliveTime;

    public DelayedUser(String name, long delayTime) {
        this.name = name;
        this.aliveTime = delayTime + System.currentTimeMillis();

    }

    @Override
    public long getDelay(TimeUnit unit) {
        long diffTime = aliveTime - System.currentTimeMillis();
        return unit.convert(diffTime, TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(@NotNull Delayed o) {
        //compareTo用在DelayedUser的排序
        return (int) (this.aliveTime - ((DelayedUser) o).getAliveTime());
    }


}
